package cn.iocoder.yudao.framework.common.util.md5;


import org.apache.commons.lang.StringUtils;

import java.security.MessageDigest;
import java.util.*;

/**
 * @Description
 * @Author allen wang
 * @Date 2020/05/14
 */
public class Md5Util {
    public static final String COMMON_NAME="数据学徒";
    /**
     * 字节数组转16进制
     * @param bytes 需要转换的byte数组
     * @return  转换后的Hex字符串
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer md5str = new StringBuffer();
        //把数组每一字节换成16进制连成md5字符串
        int digital;
        for (int i = 0; i < bytes.length; i++) {
            digital = bytes[i];
            if (digital < 0) {
                digital += 256;
            }
            if (digital < 16) {
                md5str.append("0");
            }
            md5str.append(Integer.toHexString(digital));
        }
        return md5str.toString();
    }

    public static String generateSignature(String secretId, String secretKey, String studentNumber, String studentName ){
        return generateSignature(secretId,secretKey,studentNumber,studentName,COMMON_NAME,COMMON_NAME,COMMON_NAME);
    }

        public static String generateSignature(String secretId, String secretKey, String studentNumber, String studentName,
                                           String schoolName, String majorName, String className){
        SortedMap<String, String> sortedParameters = new TreeMap<>();
        sortedParameters.put("secretId", secretId);
        sortedParameters.put("studentNumber", studentNumber);
        sortedParameters.put("studentName", studentName);
        sortedParameters.put("schoolName", schoolName);
        sortedParameters.put("majorName", majorName);
        sortedParameters.put("className", className);

        StringBuffer str = new StringBuffer();
        for (Map.Entry<String, String> entry : sortedParameters.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (StringUtils.isNotBlank(value)) {
                str.append(key + "=" + value + "&");
            }
        }
        str.append("secretKey=" + secretKey);

        String signature = "";
        try{
            //创建一个提供信息摘要算法的对象，初始化为md5算法对象
            MessageDigest md = MessageDigest.getInstance("MD5");
            //计算后获得字节数组
            byte[] bytes = md.digest(str.toString().getBytes("utf-8"));
            //把数组每一字节换成16进制连成md5字符串
            signature = bytesToHex(bytes);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return signature;
    }


    public static void main(String[] args){
        String secretId = "37669675135544524806b45cf05e0748";
        String secretKey = "4586655094761717764821a9d52d7e47";
        String studentNumber = "13200001111";
        String studentName = "张三";
        String schoolName = "计算机科学学院";
        String majorName = "大数据运用";
        String className = "202401班";

        System.out.println(generateSignature(secretId, secretKey, studentNumber, studentName, schoolName, majorName, className));
        // 668a7f53e16ce6c8d3d3f9e3363df8d4
    }
}
